//
//  RowTextViewCell.m
//  
//  jhf
//
//  Created by Mac on 2019/8/9.
//  Copyright © 2019 Mac. All rights reserved.
//

#import "RowTextViewCell.h"
#import "RowInputBaseM.h"

@interface RowTextViewCell ()

@end

@implementation RowTextViewCell


- (void)setup {
    [self.bgV addSubview:self.titleLB];
    [self.bgV addSubview:self.needLB];
    [self.bgV addSubview:self.valueTV];
    [self.bgV addSubview:self.countLB];
    
    [self.titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bgV);
        make.left.equalTo(self.bgV).offset(15);
    }];
    
    [self.needLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLB);
        make.left.equalTo(self.titleLB.mas_right).offset(2);
    }];
    
    [self.valueTV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bgV);
        make.left.equalTo(self.titleLB.mas_right).offset(8);
        make.right.equalTo(self.bgV).offset(-8);
        make.height.mas_equalTo(88);
        make.top.equalTo(self.bgV).offset(8);
    }];
    
    [self.countLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.valueTV).offset(-10);
        make.bottom.equalTo(self.valueTV).offset(-10);
    }];
    
    // 限制输入
    @weakify(self)
    [[self.valueTV.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
        @strongify(self)
        return self.model && self.valueTV.markedTextRange == nil;
    }] subscribeNext:^(NSString * _Nullable x) {
        @strongify(self)
        BindModel *bind = (BindModel *)self.model;
        RowInputBaseM *baseModel = (RowInputBaseM *)bind.model;
        NSString *str = [InputLimitHelper limitTextInput:self.valueTV conifg:baseModel];
        if (baseModel.dataKey) {
//            NSMutableDictionary *tempParam = [NSMutableDictionary dictionary];
//            [tempParam setValue:str forKey:baseModel.dataKey];
//            bind.data = tempParam;
            [bind.data setValue:str forKey:baseModel.dataKey];
        }
        if (baseModel.inputControl.maxLength && baseModel.countLBControl) {
            self.countLB.text = [NSString stringWithFormat:@"%ld/%ld", str.length, baseModel.inputControl.maxLength];
        }
        if (baseModel.monitMethod && baseModel.monitMethod.length > 0) {
            [bind.action run:baseModel.monitMethod any:bind];
        }
        baseModel.value = str;
        baseModel.inputControl.textStr = str;
    }];
}

- (void)bindTo:(__kindof id<BindProtocol>)model; {
    [super bindTo:model];
    BindModel *bind = (BindModel *)model;
    RowInputBaseM *config = (RowInputBaseM *)bind.model;
    
    if (config.titleLBControl) { /// 标题
        [config.titleLBControl controlLB:self.titleLB];
        if (config.titleLBControl.vLeftCons) {
            [self.titleLB mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.bgV).offset(config.titleLBControl.vLeftCons);
            }];
        }
        if (config.titleLBControl.vWidthCons) {
            [self.titleLB mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_offset(config.titleLBControl.vWidthCons);
            }];
        }
    }
    
    if (config.inputControl) {  /// 输入框
        [config.inputControl controlTextView:self.valueTV];
        
        // 修复本身有值的数据
        if (config.dataKey && config.inputControl.textStr && config.inputControl.textStr.clearSpace > 0) {
//            NSMutableDictionary *tempParam = [NSMutableDictionary dictionary];
//            [tempParam setValue:config.inputControl.textStr forKey:config.dataKey];
//            bind.data = tempParam;
            [bind.data setValue:config.inputControl.textStr forKey:config.dataKey];
        }
        
        // 高度
        CGFloat minHeightCons = config.inputControl.vMinHeightCons ?: 88;
        // 顶部
        CGFloat topCons = config.inputControl.vTopCons ?: 8;
        // 左侧
        CGFloat leftCons = config.inputControl.vLeftCons ?: 8;
        // 右侧
        CGFloat rightCons = config.inputControl.vRightCons ?: -8;
        
        [self.valueTV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bgV).offset(topCons);
            make.height.mas_equalTo(minHeightCons);
            make.left.equalTo(self.titleLB.mas_right).offset(leftCons);
            make.right.equalTo(self.bgV).offset(rightCons);
        }];
    }
    
    if (config.needLBControl) {  /// *比传
        [config.needLBControl controlLB:self.needLB];
        if (config.needLBControl.vLocation > 0) {
            if (config.needLBControl.vLocation == 1) { /// 左侧
                CGFloat leftCons = config.needLBControl.vRightCons ?: 2;
                [self.needLB mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(self.titleLB);
                    make.right.equalTo(self.titleLB.mas_left).offset(-leftCons);
                }];
            }else if (config.needLBControl.vLocation == 3) { /// 右侧
                CGFloat rightCons = config.needLBControl.vLeftCons ?: 2;
                [self.needLB mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(self.titleLB);
                    make.left.equalTo(self.titleLB.mas_right).offset(rightCons);
                }];
            }
        }
    }
    self.needLB.hidden = !config.needLBControl;
    
    if (config.rightImgControl) {
           self.rightIMG.hidden = false;
           [config.rightImgControl controlIMG:self.rightIMG blockBool:nil];
           CGFloat rightCons = config.rightImgControl.vRightCons ? -config.rightImgControl.vRightCons : -15;
           [self.rightIMG mas_updateConstraints:^(MASConstraintMaker *make) {
               make.right.equalTo(self.bgV).offset(rightCons);
           }];
           if (config.rightImgControl.vWidthCons && config.rightImgControl.vHeightCons) {
               CGFloat heightCons = config.rightImgControl.vHeightCons;
               CGFloat widthCons = config.rightImgControl.vWidthCons;
               [self.rightIMG mas_updateConstraints:^(MASConstraintMaker *make) {
                   make.height.mas_equalTo(heightCons);
                   make.width.mas_equalTo(widthCons);
               }];
           }
       }
       
       if (config.leftImgControl) {
           self.leftIMG.hidden = false;
           [config.leftImgControl controlIMG:self.leftIMG blockBool:nil];
           // 左侧
           CGFloat leftCons = config.leftImgControl.vLeftCons ?: 15;
           [self.leftIMG mas_updateConstraints:^(MASConstraintMaker *make) {
               make.left.equalTo(self.bgV).offset(leftCons);
           }];
           if (config.leftImgControl.vWidthCons && config.leftImgControl.vHeightCons) {
               CGFloat heightCons = config.leftImgControl.vHeightCons;
               CGFloat widthCons = config.leftImgControl.vWidthCons;
               [self.leftIMG mas_updateConstraints:^(MASConstraintMaker *make) {
                   make.height.mas_equalTo(heightCons);
                   make.width.mas_equalTo(widthCons);
               }];
           }
       }
    
     if (config.countLBControl) {
         [config.countLBControl controlLB:self.countLB];
         if (config.countLBControl.vRightCons || config.countLBControl.vBottomCons ) {
             [self.countLB mas_remakeConstraints:^(MASConstraintMaker *make) {
                 make.right.equalTo(self.valueTV).offset(-config.countLBControl.vRightCons);
                 make.bottom.equalTo(self.valueTV).offset(-config.countLBControl.vBottomCons);
             }];
         }
         self.countLB.text = [NSString stringWithFormat:@"%ld/%ld", self.valueTV.text.length, config.inputControl.maxLength];
     }
    self.countLB.hidden = !config.countLBControl;
 
    //
    //    /// 标题设置 没有 输入框左移动
    //    if (config.titleLBControl.textLeftCons.clearSpace.length == 0) {
    //        self.titleLBWidthCons.constant = 0;
    //    }else {
    //        self.titleLBWidthCons.constant = 72;
    //    }
    
    // 初始化默认参数 用于修改什么的
//    if (!bind.data && config.dataKey && config.keyValue) {
//        NSMutableDictionary *tempParam = [NSMutableDictionary dictionary];
//        [tempParam setValue:config.keyValue forKey:config.dataKey];
//        bind.data = tempParam;
//    }
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (UITextView *)valueTV {
    if (!_valueTV) {
        _valueTV = [[UITextView alloc] init];
        _valueTV.layer.masksToBounds = true;
    }
    return _valueTV;
}

- (UILabel *)needLB {
    if (!_needLB) {
        _needLB = [UILabel new];
        _needLB.backgroundColor = [UIColor redColor];
        _needLB.text = @"*";
    }
    return _needLB;
}

- (UILabel *)titleLB {
    if (!_titleLB) {
        _titleLB = [UILabel new];
    }
    return _titleLB;
}

- (UILabel *)countLB {
    if (!_countLB) {
        _countLB = [UILabel new];
        _countLB.backgroundColor = [UIColor lightGrayColor];
        _countLB.text = @"";
    }
    return _countLB;
}

@end
